package com.springboot.mybatis.service.threaddemo;

/**
 * @author sunyt31481
 * @date 2020/8/7 10:05
 * @description 带有Sleep中断线程的写法，即线程在sleep的过程中，被中断
 */
public class RightWayStopThreadWithSleep {
    public static void main(String[] args) {
        Runnable runnable = () -> {
            int num = 0;
            try {
                while (num <= 30000 && !Thread.currentThread().isInterrupted()){
                    if (num%100 == 0){
                        System.out.println(num+"是100的倍数");
                    }
                    num++;
                }
                //运行完毕，在sleep状态，收到了interrupt的请求，然后catch到了这个异常
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread.interrupt();

    }
}
